package com.example.Fragment;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Base.BaseFragment;
import com.example.activity.HotDetailActivity;
import com.example.activity.Test2Activity;
import com.example.adapter.HotItemAdapter;
import com.example.administrator.olddoctor.MainActivity;
import com.example.administrator.olddoctor.R;
import com.example.tools.adbanner.entity.AdInfo;
import com.example.tools.adbanner.view.AdBannerView;
import com.squareup.picasso.Picasso;
import com.xdandroid.simplerecyclerview.Divider;
import com.xdandroid.simplerecyclerview.OnItemClickListener;
import com.xdandroid.simplerecyclerview.SimpleRecyclerView;
import com.xdandroid.simplerecyclerview.SimpleSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * 推荐
 */
public class HotFragment extends Fragment implements View.OnClickListener {
    private Context context;
    private View view;

    private List<String> addition;
    private List<String> list;
    private HotItemAdapter hotItemAdapter;
    private SimpleRecyclerView recyclerView;
    private SimpleSwipeRefreshLayout swipeRefreshLayout;
    private int page = 1;
    private boolean hasnext = true;
    private int getState = 0;

    JCVideoPlayer.JCAutoFullscreenListener sensorEventListener;
    SensorManager sensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = MainActivity.allThis;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_hot, container, false);

//        adbanner();

//        sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();

        initView();
        setupSwipeRefreshLayout();
        setupRecyclerView();
        setupMockData();
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {


        }

    }

    private void initView() {
        list = new ArrayList<String>();
        addition = new ArrayList<String>();
        swipeRefreshLayout = (SimpleSwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        recyclerView = (SimpleRecyclerView) view.findViewById(R.id.recycler_view);
        page = 1;
        getState = 0;
    }

    private void setupSwipeRefreshLayout() {
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            public void onRefresh() {
                Log.e("banhenan","setOnRefreshListener");
                page = 1;
                hasnext = true;
                getState = 0;
                try {
                    getInfo();
                    getInfo2();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        /**
         * 启动SwipeRefreshLayout样式下拉刷新转圈。
         */
        swipeRefreshLayout.setRefreshing(true);
    }

    private void setupRecyclerView() {
        /**
         * 自定义Divider Drawable.
         */
        //recyclerView.addItemDecoration(new SimpleRecyclerView.Divider(this, R.drawable.rv_divider, false, 0, 0, 0, 0));
        recyclerView.addItemDecoration(new Divider(getActivity(), null, false, 0, 0, 0, 0));
        hotItemAdapter = new HotItemAdapter(getActivity()) {
            protected void onLoadMore(Void v) {
                Log.e("banhenan","onLoadMore");
                page = 1 + page;
                getState = 1;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getInfo();
                    }

                }, 2000);
//                try {

//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }

            protected boolean hasMoreElements(Void v) {
                return (list.size() == 10) && hasnext;
            }
        };
        hotItemAdapter.setThreshold(0);
        hotItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, View view, int position, int viewType) {

            }
        });
        hotItemAdapter.setUseMaterialProgress(true, new int[]{getResources().getColor(R.color.colorAccent)});
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(hotItemAdapter);
    }


    private void setupMockData() {
        try {
            getInfo();
            getInfo2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getInfo2() {
        hotItemAdapter.setBannerList("aa");
    }

    private void getInfo() {
        list.clear();
        for (int i = 0; i < 10; i++) {
            list.add("aa");
        }
        if (list.size() == 0) {
            hasnext = false;
        } else {
            hasnext = true;
        }
        if (getState == 0) {
            addition.clear();
            addition.addAll(list);
            hotItemAdapter.setInfoList(addition);

        } else if (getState == 1) {
            hotItemAdapter.onAddedAll(list.size());
            addition.addAll(list);
            hotItemAdapter.setInfoList(addition);
        }
        swipeRefreshLayout.setRefreshing(false);
        Log.e("banhenan","data");
    }

    @Override
    public void onResume() {
        super.onResume();
//        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onPause() {
        super.onPause();
//        sensorManager.unregisterListener(sensorEventListener);
//        JCVideoPlayer.releaseAllVideos();
    }


}
